package com.hlt.controller.admin;

import com.hlt.context.BaseContext;
import com.hlt.dto.PostCreateDTO;
import com.hlt.dto.PostsPageQueryDTO;
import com.hlt.entity.Posts;
import com.hlt.result.PageResult;
import com.hlt.result.Result;
import com.hlt.service.ICategoriesService;
import com.hlt.service.IPostsService;
import com.hlt.service.ISensitiveWordRecordService;
import com.hlt.utils.SensitiveWordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Controller("adminPostsController")
@RequestMapping("/admin/posts")
@Api(tags = "文章接口")
@Slf4j
public class PostsController {

    @Autowired
    private IPostsService postsService;
    @Autowired
    private ICategoriesService categoriesService;
    @Autowired
    private SensitiveWordUtil sensitiveWordUtil;
    @Autowired
    private ISensitiveWordRecordService sensitiveWordRecordService;  // 新增注入

    /**
     * 新增文章
     * 
     * @param posts
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增文章")
    public Result Save(@RequestBody Posts posts) {
        log.info("新增文章：{}", posts);
        Long userId = BaseContext.getCurrentId();
        posts.setUserId(Math.toIntExact(userId));
        postsService.save(posts);
        Integer categoryId = posts.getCategoryId();
        // 数量加一
        categoriesService.addCount(Long.valueOf(categoryId));
        return Result.success();
    }

    /**
     * 删除文章
     * 
     * @param postId
     * @return
     */
    @DeleteMapping("/delete/{postId}")
    @ApiOperation("删除文章")
    public Result delete(@PathVariable Integer postId) {
        log.info("删除文章：{}", postId);
        // 根据id查询文章
        Posts posts = postsService.getById(postId);
        // 数量减一
        Integer categoryId = posts.getCategoryId();
        categoriesService.subCount(Long.valueOf(categoryId));
        postsService.deletePost(postId);
        return Result.success();
    }

    /**
     * 修改文章
     * 
     * @param posts
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改文章")
    public Result update(@RequestBody Posts posts) {
        log.info("修改文章：{}", posts);
        postsService.updateById(posts);
        return Result.success();
    }

    /**
     * 根据id查询文章
     * 
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    @ApiOperation("根据id查询文章")
    public Result get(@PathVariable Long postId) {
        log.info("根据id查询文章：{}", postId);
        Posts posts = postsService.getById(postId);
        return Result.success(posts);
    }

    /**
     * 分页查询文章
     * 
     * @param postsPageQueryDTO
     * @return
     */
    @PostMapping("/page")
    @ApiOperation("分页查询文章")
    public Result page(@RequestBody PostsPageQueryDTO postsPageQueryDTO) {
        log.info("分页查询文章：{}", postsPageQueryDTO);
        PageResult pageResult = postsService.page(postsPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("发布文章")
    public Result save(@Valid @RequestBody PostCreateDTO postDTO) {
        String title = postDTO.getTitle();
        String content = postDTO.getContent();
        Long userId = BaseContext.getCurrentId();  // 获取当前用户ID

        // 检测标题敏感词

        // 将 DTO 转换为实体
        Posts post = new Posts();
        post.setTitle(title);
        post.setContent(content);
        post.setCategoryId(postDTO.getCategoryId());
        post.setStatus(1); // 默认发布状态

        // 设置用户信息
        post.setUserId(Math.toIntExact(userId));

        // 保存并处理分类计数
        postsService.save(post);
        if (postDTO.getCategoryId() != null) {
            categoriesService.addCount(Long.valueOf(postDTO.getCategoryId()));
        }

        return Result.success(post.getPostId());
    }


}
